package com.gxa.community.admin.service.system.impl;

import com.auth0.jwt.interfaces.Claim;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.community.admin.mapper.system.AdminMapper;
import com.gxa.community.admin.mapper.system.MenuMapper;
import com.gxa.community.admin.mapper.system.RoleMenuMapper;
import com.gxa.community.admin.service.system.LoginService;
import com.gxa.community.admin.util.JwtUtil;
import com.gxa.community.core.common.ResultMsg;
import com.gxa.community.core.pojo.domain.Admin;
import com.gxa.community.core.pojo.domain.Menu;
import com.gxa.community.core.pojo.domain.RoleMenu;
import com.gxa.community.core.pojo.vo.system.AdminVo;
import com.gxa.community.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 作者： Juran on 2022-07-23 18:22
 * 作者博客：iit.la
 */
@Service
public class LoginServiceImpl extends ServiceImpl<AdminMapper, Admin> implements LoginService {
    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RoleMenuMapper adminRoleMapper;

    @Autowired
    MenuMapper menuMapper;

    /**
     * 根据用户名获取用户信息
     *
     * @param username
     * @return
     */
    @Override
    public Admin queryAdminByName(String username) {
        QueryWrapper<Admin> query = new QueryWrapper<>();
        query.eq("login_act", username);
        return adminMapper.selectOne(query);
    }

    /**
     * 根据用户名获取权限信息
     *
     * @param name
     * @return
     */
    @Override
    public Set<String> queryPermissions(String name) {
        //根据用户名获取用户id
        QueryWrapper<Admin> admin = new QueryWrapper<>();
        admin.eq("login_act", name);
        Admin account = adminMapper.selectOne(admin);

        //根据用户id查询角色表拥有菜单
        QueryWrapper<RoleMenu> pre = new QueryWrapper<>();
        pre.eq("role_id", account.getId());
        List<RoleMenu> roleMenus = adminRoleMapper.selectList(pre);

        //权限
        Set<String> role = new HashSet<>();

        roleMenus.stream().forEach(p -> {
            QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
            menuQueryWrapper
                    .select("menu_code")
                    .eq("id", p.getMenuId());
            Menu menu = menuMapper.selectOne(menuQueryWrapper);
            role.add(menu.getMenuCode());
        });
        return role;
    }

    /**
     * 用户退出
     *
     * @param loginAct
     * @param userId
     * @return
     */
    @Override
    public boolean loginOut(String loginAct, int userId) {
        UpdateWrapper<Admin> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.set("login_status", 0);
        objectUpdateWrapper.eq("login_act", loginAct);
        objectUpdateWrapper.eq("id", userId);
        return update(objectUpdateWrapper);
    }

    /**
     * 修改用户登录状态
     *
     * @param username
     * @param id
     */
    @Override
    public void updateAdminLoginStatus(String username, Integer id) {
        UpdateWrapper<Admin> adminUpdateWrapper = new UpdateWrapper<>();
        adminUpdateWrapper.eq("login_act", username);
        adminUpdateWrapper.eq("id", id);
        adminUpdateWrapper.set("login_status", 1);
        update(adminUpdateWrapper);
    }

    /**
     * @param token
     * @return
     */
    @Override
    public R queryUserInfoByUserId(String token) {
        //解析token
        Claim userId = JwtUtil.getClaim(token, "userId");

        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();

        assert userId != null;
        adminQueryWrapper.eq("login_act", userId.asInt());

        Admin admin = adminMapper.selectOne(adminQueryWrapper);

        return ObjectUtils.isEmpty(admin) ?
                R.ok(ResultMsg.LOGIN_ACCOUNT_GETUSERINFO_SUCCESS.getMsg()).put("data"
                        , new AdminVo()
                                .setLoginAct(admin.getLoginAct())
                                .setLoginStatus(admin.getLoginStatus())
                                .setCreateTime(admin.getCreateTime())
                                .setId(admin.getId())
                                .setNick(admin.getNick())
                                .setPhone(admin.getPhone())
                                .setRoleId(admin.getRoleId())) :
                R.error(ResultMsg.LOGIN_ACCOUNT_GETUSERINFO_FAILURE.getMsg());
    }
}
